<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system 
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : code.php   FILE_PATH : classes\libraries\code.php
 * ....验证码
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Thu Jul 31 10:43:25 CST 2008
 */

error_reporting(E_ERROR | E_WARNING | E_PARSE);
@session_start();
$_SESSION['seccode'] = "";
$width = "60";//图片宽
$height = "20";//图片高
$len = "4";//生成几位验证码
$noise = true;//生成杂点
$noisenum = 20;//杂点数量
$image = imageCreate($width, $height);
$back = imagecolorallocate($image, 231, 227, 231); //背景色
imageFilledRectangle($image, 0, 0, $width, $height, $back);
$size = $width / $len;
if($size > $height){
	$size = $height;
}
$left = ($width - $len * ($size + $size / 10)) / $size;
/*
 * 生成随机字符串
 */
$textall=range('A','Z');
for($i = 0; $i < $len; $i++) {
	$tmptext = rand(0, 25);
	$randtext = $textall[$tmptext];
	$code .= $randtext;
}
/*
 * 画干扰点和干扰线
 */
if($noise == true){
	setnoise();
}
/*
 * 画字符串
 */
for ($i = 0; $i < strlen($code); $i++){
	imageString($image,mt_rand(4,5),$i*$width/4+mt_rand(1,5),mt_rand(1,6),$code[$i],imageColorAllocate($image,mt_rand(50,255),mt_rand(0,120),mt_rand(50,255)));
}
$_SESSION['seccode'] = $code;
@header("Content-type: image/png");
imagePng($image);
imagedestroy($image);
function setnoise()
{
	global $image, $width, $height, $back, $noisenum;
	/*
	 * 画干扰点
	 */
	for ($i=0; $i<$noisenum; $i++){
		$randColor = imageColorAllocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
		imageSetPixel($image, rand(0, $width), rand(0, $height), $randColor);
	}
	/*
	 * 画横向干扰线
	 */
	/*
	for ($i = 1; $i <= 5; $i++) {
	ImageLine($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), 5);
	}
	*/
}
?>